package com.yc.ui.admin;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Label;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.Idao.GoodsIDao;
import com.yc.dao.GoodsDao;
import com.yc.dao.LMessageDao;
import com.yc.utils.SWTUtil;
import com.yc.utils.StringUtil;

import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import java.io.ByteArrayInputStream;
import java.util.List;
import java.util.Map;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

public class LMessage extends Composite {

	private Table table;
	GoodsIDao dao = new GoodsDao();
	private byte[] bt;//图片的字节数组
	private String xingji = "";
	private Text text_reply;
	private Text text_id;
	private Text text_name;
	private TableItem item;
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public LMessage(Composite parent, int style) {
		super(parent, SWT.NONE);
		setLayout(null);
		
		table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				showValues(table);
			}
		});
		table.setBounds(45, 51, 1039, 351);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.CENTER);
		tblclmnNewColumn.setWidth(169);
		tblclmnNewColumn.setText("房间号");
		
		TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_1.setWidth(116);
		tblclmnNewColumn_1.setText("姓名");
		
		TableColumn tblclmnNewColumn_5 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_5.setWidth(130);
		tblclmnNewColumn_5.setText("星级评价");
		
		TableColumn tblclmnNewColumn_2 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_2.setWidth(295);
		tblclmnNewColumn_2.setText("评价");
		
		TableColumn tblclmnNewColumn_4 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_4.setWidth(320);
		tblclmnNewColumn_4.setText("酒店回复");
		
		text_reply = new Text(this, SWT.BORDER);
		text_reply.setBounds(603, 433, 316, 73);
		
		Label lab = new Label(this, SWT.NONE);
		lab.setBounds(236, 436, 76, 20);
		lab.setText("房间号:");
		
		text_id = new Text(this, SWT.BORDER);
		text_id.setBounds(318, 433, 97, 26);
		
		Label lblNewLabel_1 = new Label(this, SWT.NONE);
		lblNewLabel_1.setBounds(502, 436, 76, 20);
		lblNewLabel_1.setText("回复内容：");
		
		Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String id = text_id.getText().toString().trim();
				String name = text_name.getText().toString().trim();
				String reply = text_reply.getText().toString().trim();
				if(StringUtil.checkNull(id, name, reply)) {
					return;
				}
				int n = dao.reply(reply, id, name);
				if(n > 0) {
					SWTUtil.showMessage(getShell(), "回复成功！！！");
					showinfo();
				}else {
					SWTUtil.showMessage(getShell(), "回复失败！！！");
				}
			}
		});
		btnNewButton.setBounds(463, 523, 98, 30);
		btnNewButton.setText("回复");
		
		Label lab_1 = new Label(this, SWT.NONE);
		lab_1.setText("姓名:");
		lab_1.setBounds(236, 486, 76, 20);
		
		text_name = new Text(this, SWT.BORDER);
		text_name.setBounds(318, 486, 97, 26);
		showinfo();
	}
	
	private void showinfo() {
		//移除表格初始数据
		table.removeAll();
		//查询信息‘
		List<Map<String, Object>> list = dao.findAddappraise();
		TableItem tableItem = null;
		//判空
		if(null == list || list.isEmpty()) {
			SWTUtil.showMessage(getShell(), "查无评论信息");
			return;
		}
		//有信息  循环
		for(Map<String, Object> m : list) {
			tableItem = new TableItem(table, SWT.NONE);
			if(m.get("a_id").toString().equals("0")) {
				xingji = "★★★★★";
			}else if(m.get("a_id").toString().equals("1")){
				xingji = "★★★★☆";
			}else if(m.get("a_id").toString().equals("2")){
				xingji = "★★★☆☆";
			}else if(m.get("a_id").toString().equals("3")){
				xingji = "★★☆☆☆";
			}else if(m.get("a_id").toString().equals("4")){
				xingji = "★☆☆☆☆";
			}else {
				xingji = "☆☆☆☆☆";
			}
			tableItem.setText(new String[] {
				StringUtil.toString(m.get("r_id")),
				StringUtil.toString(m.get("c_name")),
				xingji,
				StringUtil.toString(m.get("a_datil")),
				StringUtil.toString(m.get("a_reply"))
			});
			bt = ( byte[] )m.get("a_image");
			// 字节数组判空
			if( null != bt) {
				Image image = SWTUtil.imageSize(120, 50, new ByteArrayInputStream(bt));
				tableItem.setImage(5, image);
			}
		}
	}
	
	public void showValues(Table table){		
		item = table.getItem(table.getSelectionIndex());
		//控件赋值
		text_id.setText(item.getText(0));
		text_name.setText(item.getText(1));
	}
}
